package org.black.phoneUtility.sms.reply.app.fragment;

import java.util.Calendar;
import java.util.Date;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.SmsReply;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class SmsReplyReplyDateTimePeriodDialogFragment extends DialogFragment
		implements View.OnClickListener {

	public static final String TAG = SmsReplyReplyDateTimePeriodDialogFragment.class
			.getName();
	public static final String ARGUMENTS_REPLY_DATE_TIME_PERIOD = "replyDateTimePeriod";

	public interface SmsReplyReplyDateTimePeriodListener {
		public void onReplyDateTimePeriodDialogPositiveClick(
				SmsReply replyDateTimeModePeriod);

		public void onReplyDateTimePeriodDialogNegativeClick();
	}

	private SmsReplyReplyDateTimePeriodListener smsReplyReplyDateTimePeriodListener;
	private SmsReply replyDateTimeModePeriod;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		replyDateTimeModePeriod = (SmsReply) getArguments().getSerializable(
				ARGUMENTS_REPLY_DATE_TIME_PERIOD);
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_reply_reply_date_time_period_dialog, null);

		Button smsReplyDateTimePeriodStartDateButton = (Button) view
				.findViewById(R.id.smsReplyDateTimePeriodStartDateButton);
		smsReplyDateTimePeriodStartDateButton.setText(DateFormat.getDateFormat(
				getActivity()).format(
				replyDateTimeModePeriod.getPeriodStartDateTime()));
		smsReplyDateTimePeriodStartDateButton.setOnClickListener(this);
		Button smsReplyDateTimePeriodStartTimeButton = (Button) view
				.findViewById(R.id.smsReplyDateTimePeriodStartTimeButton);
		smsReplyDateTimePeriodStartTimeButton.setText(DateFormat.getTimeFormat(
				getActivity()).format(
				replyDateTimeModePeriod.getPeriodStartDateTime()));
		smsReplyDateTimePeriodStartTimeButton.setOnClickListener(this);

		Button smsReplyDateTimePeriodEndDateButton = (Button) view
				.findViewById(R.id.smsReplyDateTimePeriodEndDateButton);
		smsReplyDateTimePeriodEndDateButton.setText(DateFormat.getDateFormat(
				getActivity()).format(
				replyDateTimeModePeriod.getPeriodEndDateTime()));
		smsReplyDateTimePeriodEndDateButton.setOnClickListener(this);
		Button smsReplyDateTimePeriodEndTimeButton = (Button) view
				.findViewById(R.id.smsReplyDateTimePeriodEndTimeButton);
		smsReplyDateTimePeriodEndTimeButton.setText(DateFormat.getTimeFormat(
				getActivity()).format(
				replyDateTimeModePeriod.getPeriodEndDateTime()));
		smsReplyDateTimePeriodEndTimeButton.setOnClickListener(this);
		builder.setView(view);
		builder.setTitle(R.string.sms_reply_add_edit_date_time_period)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								replyDateTimeModePeriod
										.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_PERIOD_TYPE);
								smsReplyReplyDateTimePeriodListener
										.onReplyDateTimePeriodDialogPositiveClick(replyDateTimeModePeriod);
							}
						}).setNegativeButton(android.R.string.cancel, null);
		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyDateTimePeriodListener = (SmsReplyReplyDateTimePeriodListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyReplyDateTimePeriodListener");
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.smsReplyDateTimePeriodStartDateButton
				|| v.getId() == R.id.smsReplyDateTimePeriodEndDateButton) {
			SmsReplyReplyDateTimePeriodDatePickerDialogFragment smsReplyReplyDateTimePeriodDatePickerDialogFragment = new SmsReplyReplyDateTimePeriodDatePickerDialogFragment();
			Bundle bundle = new Bundle();
			int year, month, date;
			boolean isStart;

			if (v.getId() == R.id.smsReplyDateTimePeriodStartDateButton) {
				isStart = true;
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(replyDateTimeModePeriod
						.getPeriodStartDateTime().getTime());
				year = calendar.get(Calendar.YEAR);
				month = calendar.get(Calendar.MONTH);
				date = calendar.get(Calendar.DATE);
			} else {
				isStart = false;
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(replyDateTimeModePeriod
						.getPeriodEndDateTime().getTime());
				year = calendar.get(Calendar.YEAR);
				month = calendar.get(Calendar.MONTH);
				date = calendar.get(Calendar.DATE);

			}
			bundle.putBoolean(
					SmsReplyReplyDateTimePeriodDatePickerDialogFragment.ARGUMENTS_IS_BEGIN,
					isStart);
			bundle.putInt(
					SmsReplyReplyDateTimePeriodDatePickerDialogFragment.ARGUMENTS_YEAR,
					year);
			bundle.putInt(
					SmsReplyReplyDateTimePeriodDatePickerDialogFragment.ARGUMENTS_MONTH,
					month);
			bundle.putInt(
					SmsReplyReplyDateTimePeriodDatePickerDialogFragment.ARGUMENTS_DATE,
					date);
			smsReplyReplyDateTimePeriodDatePickerDialogFragment
					.setArguments(bundle);
			smsReplyReplyDateTimePeriodDatePickerDialogFragment.show(
					getFragmentManager(),
					SmsReplyReplyDateTimePeriodDatePickerDialogFragment.TAG);
		} else if (v.getId() == R.id.smsReplyDateTimePeriodStartTimeButton
				|| v.getId() == R.id.smsReplyDateTimePeriodEndTimeButton) {
			SmsReplyReplyDateTimePeriodTimePickerDialogFragment smsReplyReplyDateTimePeriodTimePickerDialogFragment = new SmsReplyReplyDateTimePeriodTimePickerDialogFragment();
			Bundle bundle = new Bundle();
			int hourOfDay, minute;
			boolean isStart;

			if (v.getId() == R.id.smsReplyDateTimePeriodStartTimeButton) {
				isStart = true;
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(replyDateTimeModePeriod
						.getPeriodStartDateTime().getTime());
				hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
				minute = calendar.get(Calendar.MINUTE);
			} else {
				isStart = false;
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(replyDateTimeModePeriod
						.getPeriodEndDateTime().getTime());
				hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
				minute = calendar.get(Calendar.MINUTE);
			}
			bundle.putBoolean(
					SmsReplyReplyDateTimePeriodTimePickerDialogFragment.ARGUMENTS_IS_BEGIN,
					isStart);
			bundle.putInt(
					SmsReplyReplyDateTimePeriodTimePickerDialogFragment.ARGUMENTS_HOUR_OF_DAY,
					hourOfDay);
			bundle.putInt(
					SmsReplyReplyDateTimePeriodTimePickerDialogFragment.ARGUMENTS_MINUTE,
					minute);
			smsReplyReplyDateTimePeriodTimePickerDialogFragment
					.setArguments(bundle);
			smsReplyReplyDateTimePeriodTimePickerDialogFragment.show(
					getFragmentManager(),
					SmsReplyReplyDateTimePeriodTimePickerDialogFragment.TAG);
		}
	}

	public void handlePeriodDatePickerSelection(boolean isBegin, int year,
			int month, int dayOfMonth) {
		Date date = null;
		if (isBegin == true) {
			date = replyDateTimeModePeriod.getPeriodStartDateTime();
		} else {
			date = replyDateTimeModePeriod.getPeriodEndDateTime();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date.getTime());
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DATE, dayOfMonth);
		if (isBegin == true) {
			replyDateTimeModePeriod.setPeriodStartDateTime(calendar.getTime());

			((Button) getDialog().findViewById(
					R.id.smsReplyDateTimePeriodStartDateButton))
					.setText(DateFormat.getDateFormat(getActivity()).format(
							replyDateTimeModePeriod.getPeriodStartDateTime()));
		} else {
			replyDateTimeModePeriod.setPeriodEndDateTime(calendar.getTime());
			((Button) getDialog().findViewById(
					R.id.smsReplyDateTimePeriodEndDateButton))
					.setText(DateFormat.getDateFormat(getActivity()).format(
							replyDateTimeModePeriod.getPeriodEndDateTime()));
		}

	}

	public void handlePeriodTimePickerSelection(boolean isBegin, int hourOfDay,
			int minute) {
		Date date = null;
		if (isBegin == true) {
			date = replyDateTimeModePeriod.getPeriodStartDateTime();
		} else {
			date = replyDateTimeModePeriod.getPeriodEndDateTime();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date.getTime());
		calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
		calendar.set(Calendar.MINUTE, minute);
		if (isBegin == true) {
			replyDateTimeModePeriod.setPeriodStartDateTime(calendar.getTime());

			((Button) getDialog().findViewById(
					R.id.smsReplyDateTimePeriodStartTimeButton))
					.setText(DateFormat.getTimeFormat(getActivity()).format(
							replyDateTimeModePeriod.getPeriodStartDateTime()));
		} else {
			replyDateTimeModePeriod.setPeriodEndDateTime(calendar.getTime());
			((Button) getDialog().findViewById(
					R.id.smsReplyDateTimePeriodEndTimeButton))
					.setText(DateFormat.getTimeFormat(getActivity()).format(
							replyDateTimeModePeriod.getPeriodEndDateTime()));
		}

	}
}
